﻿using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour
{
	// Use this for initialization
	public Transform playerPrefab;

	void OnServerInitialized()
	{
		SpawnPlayer();
	}

	void OnConnectedToServer()
	{
		SpawnPlayer();
	}

	void SpawnPlayer()
	{
		Transform myNewTrans = (Transform) Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
	}

	void OnPlayerDisconnected(NetworkPlayer player)
	{
		Debug.Log("Clean up after player " + player);
		Network.RemoveRPCs(player);
		Network.DestroyPlayerObjects(player);
	}

	void OnDisconnectedFromServer(NetworkDisconnection info)
	{
		Debug.Log("Clean up a bit after server quit");
		Network.RemoveRPCs(Network.player);
		// Network.DestroyPlayerObjects(Network.player);
		Application.LoadLevel(Application.loadedLevel);
	}
}